package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户人群业务相关
 */
@Slf4j
@Tag(name = "用户人群管理")
@RestController
public class UserGroupController {
    @Autowired
    private UserGroupService userGroupService;



    @Operation(summary = "人群数据分页列表")
    @PostMapping("/admin/basic/userGroup/list")
    public Result<PageResult<UserGroupVO>> pageList(@RequestBody UserGroupQuery query)
        throws BusinessException{
        log.debug("查询人群数据分页列表");
        PageResult<UserGroupVO> voPage = userGroupService.pageList(query);
        log.debug("查询人群数据分页列表成功，出参:{}",voPage);
        return new Result<>(voPage);
    }

    @Operation(summary = "保存人群数据")
    @PostMapping("/admin/basic/userGroup/save")
    public Result<Void> save(@RequestBody UserGroupParam param)throws BusinessException {
        log.debug("保存人群数据,入参:{}",param);
        userGroupService.save(param);
        log.debug("保存人群数据成功,入参:{}",param);
        return Result.success();
    }

    @Operation(summary = "查看人群详情")
    @GetMapping("/admin/basic/userGroup/detail/{id}")
    public Result<UserGroupParam> detail(UserGroupParam param) throws BusinessException {
        log.debug("查询人群数据详情,入参:{}",param);
        UserGroupParam userGroupParam = userGroupService.detail(param);
        log.debug("查询人群数据详情成功，出参:{}",userGroupParam);
        return new Result<>(userGroupParam);
    }

    @Operation(summary = "编辑人群数据")
    @PostMapping("/admin/basic/userGroup/update")
    public Result<Void> update(@RequestBody UserGroupParam param) throws BusinessException {
        log.debug("编辑人群数据,入参:{}", param);
        userGroupService.save(param);  // 复用 save 方法，根据 id 判断新增或更新
        log.debug("编辑人群数据成功,入参:{}", param);
        return Result.success();
    }

    @Operation(summary = "更新人群数据状态")
    @PostMapping("/admin/basic/userGroup/updateStatus")
    public Result<Void> updateStatus(@RequestBody UserGroupParam param) throws BusinessException {
        log.debug("更新人群数据状态,入参:{}", param);
        userGroupService.update(param);
        log.debug("更新人群数据状态成功,入参:{}", param);
        return Result.success();
    }

    @Operation(summary = "删除人群数据")
    @PostMapping("/admin/basic/userGroup/delete")
    public Result<Void> delete(@RequestBody UserGroupParam param) throws BusinessException {
        log.debug("删除人群数据,入参:{}", param);
        userGroupService.delete(param);
        log.debug("删除人群数据成功,入参:{}", param);
        return Result.success();
    }

}


















